package com.example.pkmedical.Activity.warning;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.pkmedical.APP.MyApp;
import com.example.pkmedical.Base.BaseActivity;
import com.example.pkmedical.MainActivity;
import com.example.pkmedical.R;
import com.example.pkmedical.Utils.CommandUtil;
import com.example.pkmedical.Utils.SerialProtUtil;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

import static com.example.pkmedical.Utils.TimeUtils.generateTime;

public class LeaveingBedActivity extends BaseActivity {

    @BindView(R.id.headtilt_text)
    TextView headtiltText;
    @BindView(R.id.id_leaving_bed_huifu_button)
    Button buttonNovoicedRecovery;
    @BindView(R.id.id_leaving_bed_pause_button)
    Button buttonNovoicedPause;
    @BindView(R.id.id_leaving_bed_close_button)
    Button buttonNovoicedClose;
    @BindView(R.id.leaving_progress_layout)
    LinearLayout leavingProgressLayout;
    @BindView(R.id.leaving_waiting_layout)
    LinearLayout leavingWaitingLayout;
    int timeLong = 30;
    @BindView(R.id.id_leaving_bed_progress)
    ProgressBar idLeavingBedProgress;
    @BindView(R.id.id_leaving_bed_progress_text)
    TextView idLeavingBedProgressText;
    @BindView(R.id.id_leaving_bed_pause_button_text)
    TextView idLeavingBedPauseButtonText;
    @BindView(R.id.id_leaving_bed_progress_title)
    TextView idLeavingBedProgressTitle;
    private int mType = 1;
    private int mLaseType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leaveingbed);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        switch (CommandUtil.LEAVEBED_TYPE) {
//            case 0:
//                mLaseType = 0;
//                headtiltText.setText(getString(R.string.warning_离床));
//                break;
            case 1:
                mLaseType = 1;
                headtiltText.setText(getString(R.string.warning_no_noice_position));
                break;
            case 2:
                mLaseType = 2;
                headtiltText.setText(getString(R.string.warning_no_noice_leaveing));
                break;
            case 3:
                mLaseType = 3;
                headtiltText.setText(getString(R.string.warning_no_noice_leaved));
                break;
        }

    }

    @Override
    public void initView() {
        mType = 1;
        idLeavingBedProgress.setMax(timeLong);
        TimeCount();
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void showDialog(String... msg) {

    }

    @OnClick({R.id.id_leaving_bed_huifu_button, R.id.id_leaving_bed_pause_button, R.id.id_leaving_bed_close_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.id_leaving_bed_huifu_button:
                SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.GET_WEIGHT_LEAVE);
                startActivity(new Intent(MyApp.getAppContext(), MainActivity.class));
                LeaveingBedActivity.this.finish();
                break;
            case R.id.id_leaving_bed_pause_button:
                if (1 == mType) {
                    switch (CommandUtil.LEAVEBED_TYPE) {
                        case 1:
                            headtiltText.setText(getString(R.string.warning_paused_one));
                            break;
                        case 2:
                            headtiltText.setText(getString(R.string.warning_paused_two));
                            break;
                        case 3:
                            headtiltText.setText(getString(R.string.warning_paused_three));
                            break;

                    }
                    idLeavingBedProgressTitle.setText(getText(R.string.leavebed_close_ing_10));
                    buttonNovoicedPause.setText(getString(R.string.warning_no_voice));
                    idLeavingBedPauseButtonText.setText(getString(R.string.warning_pause_leave_bed_thirty));
                    timeLong = 600;
                    idLeavingBedProgress.setMax(600);
                    mType = 2;

                } else {
                    switch (CommandUtil.LEAVEBED_TYPE) {
                        case 1:
                            headtiltText.setText(getString(R.string.warning_no_voiced_one));
                            break;
                        case 2:
                            headtiltText.setText(getString(R.string.warning_no_voiced_two));
                            break;
                        case 3:
                            headtiltText.setText(getString(R.string.warning_no_voiced_three));
                            break;

                    }
                    idLeavingBedProgressTitle.setText(getText(R.string.leavebed_close_ing_30));
                    buttonNovoicedPause.setText(getString(R.string.warning_pause));
                    idLeavingBedPauseButtonText.setText(getString(R.string.warning_pause_leave_bed));
                    timeLong = 30;
                    idLeavingBedProgress.setMax(30);
                    mType = 1;
                }
                TimeCount();
                break;
            case R.id.id_leaving_bed_close_button:
                CommandUtil.LEAVEBED_TYPE = 0;
//                startActivity(new Intent(LeaveingBedActivity.this, MainActivity.class));
                LeaveingBedActivity.this.finish();
                break;
        }
    }

    Disposable mTimeCountDisposable;
    int nowProgress = 0;


    /**
     * 倒计时
     */
    public void TimeCount() {
//        switch (mType) {
//            case 1:
//
//                break;
//            case 2:
//
//                break;
//        }
        nowProgress = 0;
        idLeavingBedProgress.setProgress(0);
        idLeavingBedProgressText.setText("0");
        if (mTimeCountDisposable != null) {
            //取消上一次的订阅
            mTimeCountDisposable.dispose();
        }
//        //0-count依次输出，延时0s执行，每1s发射一次。
        mTimeCountDisposable = Flowable.intervalRange(0, timeLong, 0, 1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(aLong -> {
                    idLeavingBedProgressText.setText(generateTime(timeLong -= 1));
                    idLeavingBedProgress.setProgress(nowProgress += 1);
                })
                .doOnComplete(() -> {
                    if (mTimeCountDisposable != null) {
                        mTimeCountDisposable.dispose();
                    }
                    SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.GET_WEIGHT_LEAVE);
                    startActivity(new Intent(MyApp.getAppContext(), MainActivity.class));
                    LeaveingBedActivity.this.finish();
                })
                .subscribe();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mTimeCountDisposable != null) {
            //取消上一次的订阅
            mTimeCountDisposable.dispose();
        }
    }
}
